package com.kotlin.demo

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode
import androidx.lifecycle.*
import com.kotlin.demo.bean.ArticleData
import com.kotlin.demo.net.ApiResponse
import com.kotlin.demo.net.RetrofitClient
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.callbackFlow
import org.koin.android.viewmodel.ext.android.viewModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.lang.reflect.Proxy
import kotlin.system.measureTimeMillis


class MainActivity : AppCompatActivity() {

    //val viewModel: UserViewModel by lazy { ViewModelProvider.NewInstanceFactory().create(UserViewModel::class.java) }

    val viewModel: UserViewModel by viewModel()

    public val TAG = "MainActivity"

    init {
        Log.i(TAG, ": init ")
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //testAsync()

        viewModel.datas.observe(this, Observer {
            //http 错误
            if (it.code == ApiResponse.ERROR_CODE) {
                Log.i(TAG, ": ${it.msg}")
                return@Observer
            }

            val body = it.body

            body?.let { bean ->
                //接口错误
                if (bean.errorCode != 200) {
                    Log.i(TAG, ": ${bean.errorMsg}");
                    return@Observer
                }
                Log.i(TAG, ": ${it.toString()}");
            }
        })

    }


    override fun onResume() {
        super.onResume()
        Log.i(TAG, ": onResume");
    }


    fun test2() {
        runBlocking() {

            Log.i(TAG, "start ${Thread.currentThread().name} ")


            delay(100)

            var name = "2"

            launch {
                name = "3"
                Log.i(TAG, "launch ${Thread.currentThread().name} ")
            }


            val coroutineScope = coroutineScope {

                val a = async(Dispatchers.IO) {
                    Log.i(TAG, ": ${System.currentTimeMillis()}")
                    6
                }.await()


                val b = withContext(Dispatchers.Default) {
                    Log.i(TAG, ": ${System.currentTimeMillis()}")
                    name()
                }

                "$a  $b"
            }

            Log.i(TAG, ": $coroutineScope     ${Thread.currentThread().name} ")



            runOnUiThread {
                Log.i(TAG, ":runOnUiThread 结果 $name ")
                Log.i(TAG, "runOnUiThread  ${Thread.currentThread().name} ");
            }

        }

        Log.i(TAG, ": end")
    }


    fun test() {
        Log.i(TAG, ": 1")
        runBlocking(Dispatchers.IO) {

            launch {
                Thread.sleep(1000)
                Log.i(TAG, ": 3  ${Thread.currentThread().name}")
            }


            launch() {
                Log.i(TAG, ": 4  ${Thread.currentThread().name}")
            }

            val str = withContext(Dispatchers.Default) { name() }


            Log.i(TAG, ": 6  $str    ${Thread.currentThread().name}")


            Log.i(TAG, ": 5  ${Thread.currentThread().name}")
        }

        Log.i(TAG, ": 2")
    }

    fun name(): String {
        return "zf"
    }


    fun flow() {

    }

    fun testAsync() {
        runBlocking {
            val mill = measureTimeMillis {

                val name = async { getName() }
                val age = async { getAge() }

                val awaitAll = awaitAll(name, age)
                for (item in awaitAll) {


                    Log.i(TAG, "返回->: $item")
                }
            }
            Log.i(TAG, "耗时---->: $mill.");
        }
    }

    suspend fun getName(): String {
        delay(1000)
        return "name";
    }

    suspend fun getAge(): Int {
        delay(1000)
        return 11;
    }


}
